package main.java.indi.zyj.sword;

import main.java.indi.zyj.hot100.base.Node;

import java.util.HashMap;
import java.util.Map;

public class CopyRandomList {

    public Node copyRandomList(Node head) {

        Map<Node, Node> directory = new HashMap<>();
        Node curNode = head;

        while (curNode != null) {
            directory.put(curNode, new Node(curNode.val));
            curNode = curNode.next;
        }

        curNode = head;

        while (curNode != null) {
            directory.get(curNode).next = directory.get(curNode.next);
            directory.get(curNode).random = directory.get(curNode.random);
            curNode = curNode.next;
        }

        return directory.get(head);

    }

}
